#! /usr/bin/env python3
import re
import subprocess

output = subprocess.check_output(['ovs-vsctl', 'show']).decode('utf-8')

bridge_regex = re.compile(r'Bridge (\w+)')
port_regex = re.compile(r'Port (\S+)')
interface_regex = re.compile(r'Interface (\S+)')
port_speed_cmd = 'ovs-vsctl set Interface {} options:speeds={}'

bridges = bridge_regex.findall(output)
ports = port_regex.findall(output)
interfaces = interface_regex.findall(output)

for port in ports:
    if '-' in port and port.startswith('p'):
        if port in interfaces:
            interface_index = interfaces.index(port)
            interface_type = output.split('\n')[interface_index + 1].strip().split(':')[1].strip()
            print(interface_type)
            if interface_type == 'patch':
                continue
        if 'h' in port:
            speed = 'auto'
        else:
            speed = '1G'
        cmd = port_speed_cmd.format(port, speed)
        # subprocess.call(cmd, shell=True)
        # print(f"Set speed for port {port} to {speed}")

print("Speed limit configuration completed.")
